Page({
  data: {
    currentSection: 'textToSpeech',
    language: 'zh',
    textInput: '你好，欢迎使用 Fish Speech！这是我们的文本转语音技术演示。',
    cloneTextInput: '这是语音克隆部分。请输入要使用克隆语音合成的文本。',
    transcriptionOutput: '',
    audioFileName: '',
    voices: [
      { id: 'default', name: '默认声音' },
      { id: 'male', name: '男声' },
      { id: 'female', name: '女声' }
    ],
    cloneVoices: [
      { id: 'default', name: '克隆声音' },
      { id: 'voice1', name: '声音 1' },
      { id: 'voice2', name: '声音 2' }
    ],
    selectedVoice: { id: 'default', name: '默认声音' },
    selectedCloneVoice: { id: 'default', name: '克隆声音' }
  },

  onLoad() {
    // Load saved language preference
    const savedLanguage = wx.getStorageSync('language') || 'zh';
    this.setData({
      language: savedLanguage
    });
    
    // Update text based on language
    this.updateTextContent();
  },

  switchSection(e) {
    const section = e.currentTarget.dataset.section;
    this.setData({
      currentSection: section
    });
  },

  toggleLanguage() {
    const newLanguage = this.data.language === 'zh' ? 'en' : 'zh';
    this.setData({
      language: newLanguage
    });
    
    // Save language preference
    wx.setStorageSync('language', newLanguage);
    
    // Update text content
    this.updateTextContent();
  },

  updateTextContent() {
    const lang = this.data.language;
    if (lang === 'zh') {
      this.setData({
        textInput: '你好，欢迎使用 Fish Speech！这是我们的文本转语音技术演示。',
        cloneTextInput: '这是语音克隆部分。请输入要使用克隆语音合成的文本。'
      });
    } else {
      this.setData({
        textInput: 'Hello, welcome to Fish Speech! This is a demo of our text to speech technology.',
        cloneTextInput: 'This is the voice cloning section. Enter text to synthesize with cloned voice.'
      });
    }
  },

  onTextInput(e) {
    this.setData({
      textInput: e.detail.value
    });
  },

  onCloneTextInput(e) {
    this.setData({
      cloneTextInput: e.detail.value
    });
  },

  onVoiceChange(e) {
    const selectedIndex = e.detail.value;
    this.setData({
      selectedVoice: this.data.voices[selectedIndex]
    });
  },

  onCloneVoiceChange(e) {
    const selectedIndex = e.detail.value;
    this.setData({
      selectedCloneVoice: this.data.cloneVoices[selectedIndex]
    });
  },

  speakText() {
    if (!this.data.textInput.trim()) {
      wx.showToast({
        title: this.data.language === 'zh' ? '请输入文本' : 'Please enter text',
        icon: 'none'
      });
      return;
    }
    
    wx.showToast({
      title: this.data.language === 'zh' ? '正在播放...' : 'Speaking...',
      icon: 'none'
    });
  },

  stopText() {
    wx.showToast({
      title: this.data.language === 'zh' ? '已停止' : 'Stopped',
      icon: 'none'
    });
  },

  speakCloneText() {
    if (!this.data.cloneTextInput.trim()) {
      wx.showToast({
        title: this.data.language === 'zh' ? '请输入文本' : 'Please enter text',
        icon: 'none'
      });
      return;
    }
    
    wx.showToast({
      title: this.data.language === 'zh' ? '正在播放克隆语音...' : 'Speaking with cloned voice...',
      icon: 'none'
    });
  },

  stopCloneText() {
    wx.showToast({
      title: this.data.language === 'zh' ? '已停止' : 'Stopped',
      icon: 'none'
    });
  },

  chooseAudioFile() {
    wx.chooseMessageFile({
      count: 1,
      type: 'audio',
      success: (res) => {
        const tempFilePaths = res.tempFiles;
        this.setData({
          audioFileName: tempFilePaths[0].name
        });
      },
      fail: () => {
        wx.showToast({
          title: this.data.language === 'zh' ? '选择文件失败' : 'Failed to choose file',
          icon: 'none'
        });
      }
    });
  },

  transcribeAudio() {
    if (!this.data.audioFileName) {
      wx.showToast({
        title: this.data.language === 'zh' ? '请先选择音频文件' : 'Please choose an audio file first',
        icon: 'none'
      });
      return;
    }
    
    wx.showLoading({
      title: this.data.language === 'zh' ? '转录中...' : 'Transcribing...'
    });
    
    // Simulate transcription process
    setTimeout(() => {
      wx.hideLoading();
      const lang = this.data.language;
      this.setData({
        transcriptionOutput: lang === 'zh' ? 
          `转录结果来自 ${this.data.audioFileName}:\n\n这是一个模拟的转录结果。在实际应用中，这里会显示上传音频文件的实际转录文本。` : 
          `Transcribed text from ${this.data.audioFileName}:\n\nThis is a simulated transcription result. In a real application, this would contain the actual transcribed text from the uploaded audio file.`
      });
      
      wx.showToast({
        title: lang === 'zh' ? '转录完成' : 'Transcription completed',
        icon: 'success'
      });
    }, 2000);
  },

  stopTranscribe() {
    wx.showToast({
      title: this.data.language === 'zh' ? '已停止' : 'Stopped',
      icon: 'none'
    });
  }
});